﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Threading;
using System.Threading.Tasks;
using WBrowser.Properties;
using WCrawlerLib;


namespace WBrowser.PostForms
{
    public partial class ucPostThread : UserControl
    {
        public WBrowsers WBrowserForm;

        public ucPostThread()
        {
            InitializeComponent();
        }

        public void GetSelectedValues(out List<int> forumIdList, out int postId)
        {
            forumIdList = new List<int>();
            postId = (int)dataGridViewPost.Rows[dataGridViewPost.CurrentCell.RowIndex].Cells[0].Value;
            foreach (DataGridViewRow dr in dataGridViewForums.Rows)
            {
                if (dr.Cells["Select"].Value != null)
                {
                    if ((bool)dr.Cells["Select"].Value == true)
                    {
                        int forumId = (int)dataGridViewForums.Rows[dr.Index].Cells[1].Value;
                        forumIdList.Add(forumId);
                    }
                }
            }
        }

        //public void Filter()
        //{
        //    forumBindingSource.Filter = "";
        //    forumBindingSource.Filter += "IsApproved = " + checkBoxApprovedForum.Checked.ToString();
        //    if (comboBox1.SelectedValue != null)
        //        forumBindingSource.Filter += " and GroupId = " + comboBox1.SelectedValue;
        //}

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Filter();
        }

        private void checkBoxApprove_CheckedChanged(object sender, EventArgs e)
        {
            //Filter();
        }

        private void ucPostThread_Load(object sender, EventArgs e)
        {
            WCrawlerLib.AppSettings.my_mpconfigConnectionString =
                WBrowser.Properties.Settings.Default.my_mpconfigConnectionString;

            ForumGroup objForumGroup = new ForumGroup();
            comboBox1.DataSource = objForumGroup.GetAll();

            WCrawlerLib.Post objPost = new WCrawlerLib.Post();
            dataGridViewPost.DataSource = objPost.GetAll();

            

            //Forum objForum = new Forum();
            //dataGridViewForums.DataSource = objForum.Get;

        }

        private void btnAutoFill_Click(object sender, EventArgs e)
        {

        }

        private void checkBoxApprovedPost_CheckedChanged(object sender, EventArgs e)
        {
            //postBindingSource.Filter = "IsApproved=" + checkBoxApprovedPost.Checked;
            //dataGridViewPost.DataSource = postBindingSource;
        }

        private void dataGridViewPost_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            Forum obj = new Forum();
            var forumsQuery = obj.GetPostedForums((int)dataGridViewPost.Rows[e.RowIndex].Cells["PostId"].Value,0)
                                 .WhereGroup(comboBox1.SelectedValue.ToString())
                                 .IsApproved(checkBoxApprovedForum.Checked);
            dataGridViewForums.DataSource = forumsQuery;
        }
    }
}
